import time

import requests
import json
from setenv import setLotteryDir
from setenv import getFileDir
from setenv import getFileName


def fetch_lotto_numbers(url):

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, sdch',
        'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
    }

    response = requests.get(url, headers=headers)
    setLotteryDir()
    ssqdir = getFileDir()
    ssqname = getFileName("ssqfilename")
    if response.status_code == 200:
        html = response.text
        # print(html)
        # 假设中奖号码在HTML中的某个特定标签内，例如：<div class="lotto-numbers">
        data = json.loads(str(html))
        # print(data)
        # print(data.get('value').get('lastPoolDraw').get('lotteryDrawNum'))
        # print(data.get('value').get('lastPoolDraw').get('lotteryDrawResult'))
        # print(data.get('value').get('lastPoolDraw').get('lotteryDrawTime'))
        # print(data.get('value').get('lastPoolDraw').get('lotteryGameName'))
        # print(data.get('value').get('lastPoolDraw').get('lotteryGameNum'))
        # lotteryDrawNum = data.get('value').get('lastPoolDraw').get('lotteryDrawNum')
        # lotteryDrawResult = data.get('value').get('lastPoolDraw').get('lotteryDrawResult')
        # lotteryDrawTime = data.get('value').get('lastPoolDraw').get('lotteryDrawTime')
        # lotteryGameName = data.get('value').get('lastPoolDraw').get('lotteryGameName')
        # lotteryGameNum = data.get('value').get('lastPoolDraw').get('lotteryGameNum')
        for i in range(0,len(data.get('result'))):
            # print(data.get('result')[i].get('code'))
            # print(data.get('result')[i].get('red'))
            # print(data.get('result')[i].get('blue'))
            # print(data.get('result')[i].get('date').split('('))
            # print(data.get('result')[i].get('name'))
            lotteryDrawNum = data.get('result')[i].get('code')
            lotteryDrawResult = data.get('result')[i].get('red').split(',')
            blue = data.get('result')[i].get('blue')
            lotteryDrawResult.append(blue)
            lotteryDrawTime = data.get('result')[i].get('date').split('(')
            lotteryGameName = data.get('result')[i].get('name')



    # 使用上下文管理器打开文件
    with open( str(ssqdir) + "/" + str(ssqname) , "w") as file:
    # 写入数据
        for var in lotteryDrawResult:
            file.write(str(var) + " ")
        file.write('\n')

    file.close
    print(lotteryGameName,"开奖期数：", lotteryDrawNum)
    print(lotteryGameName,"开奖时间：", lotteryDrawTime[0])
    print(lotteryGameName,"开奖号码：", lotteryDrawResult)

    return None

if __name__ == '__main__':
    for i in range(1,2):
        url = 'https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&pageNo='+str(i)+'&pageSize=1&systemType=PC'  # 替换为实际的大乐透中奖号码公布页面URL
        # print(url)
        numbers = fetch_lotto_numbers(url)
        time.sleep(2)
